//
// See the file COPYING for redistribution information.
//

#ifndef __NAIPAX_HISTORY_HPP__
#define __NAIPAX_HISTORY_HPP__

#include <stdint.h>
#include <time.h>
#include <db_cxx.h>

namespace Naipax {
    /** Data history
     *
     * This class utilizes the Digest module to calculate a checksum
     * which will be inserted into database on behalf of the original
     * data.
     */
    class History {
    public:
        /** Constructor
         *
         * @param pFilename History file
         * @param CacheSize Default cache size is 4 mega-bytes.
         * @exception Error::INVALID_ARGUMENT if pFilename is NULL.
         * @exception Error::CANNOT_OPEN_DB if the history database cannot
         *            be opened or the cache size cannot be set.
         * @exception Error::CANNOT_INIT_HISTORY If any other error occurs.
         */
        History(const char *pFilename, uint32_t CacheSize = 1048576 * 4);

        /** Destructor
         *
         * @exception Error::CANNOT_CLOSE_DB if the history database cannot
         *            be closed.
         */
        ~History();

        /** Insert data into history.
         *
         * @param &rData Data to be inserted.
         * @param Timeout Default timeout value is 30 days.
         * @exception Error::CANNOT_PUT_DATA If rData cannot be inserted
         *            into database
         */
        void Add(const std::string &rData, time_t Timeout = 86400*30);

        /** Check if data lives in history.
         *
         * @return true if a hit is successfully found and data does
         *         not exceeds its timeout.
         * @return false otherwise
         */
        bool Has(const std::string &rData);

    private:
        Db *_mpDb;
    };
};

#endif // __NAIPAX_HISTORY_HPP__
